package com.dongdongshop.util;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.quartz.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class JobBirExecute implements Job {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //生成4位数的手机验证码
        String code = RandomStringUtils.randomNumeric(4);
        JobKey key = context.getJobDetail().getKey();

        JobDataMap dataMap = context.getMergedJobDataMap();
        String name = dataMap.getString("name");

        //给用户发送邮箱贺卡
        String phoneByCode=""+name+","+code+"";
        rocketMQTemplate.convertAndSend("sms_code",phoneByCode);
        System.out.println(new Date() + " : 定时任务执行...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("Instance " + key + " of DumbJob says: " + name );
        System.out.println(new Date() + " : 定时任务结束...");
    }
}
